<?php

/**
 * adminclusters actions.
 *
 * @package    retailbrain
 * @subpackage adminclusters
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class adminclustersActions extends sfActions {
    
    /**
     * Executes clusters action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Clusters');
        $this->breadcrumbs = array(array("Administración", "admin/index"), array("Clusters", "adminclusters/index"));

        $q = Doctrine_Query::create()
                ->from('Clusters c')
                ->orderBy('c.name desc');

        $this->clusters = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');
    }

    /**
     * Executes editClusters action
     *
     * @param sfRequest $request A request object
     */
    public function executeEditClusters(sfWebRequest $request) {

        $id = $this->getRequestParameter('id');

        $this->getResponse()->setSlot('title', 'Administración : Editar Clusters');
        $this->breadcrumbs = array(array("Administración", "admin/index"), array("Clusters", "adminclusters/index"), array("Editar Cluster", "adminclusters/editClusters?id=$id"));


        /** Channel * */
        $q = Doctrine_Query::create()
                ->from('Clusters c')
                ->where('c.id_cluster = ?', array($id))
                ->fetchArray();

        $this->cluster = $q;
    }

    /**
     * Executes addClusters action
     *
     * @param sfRequest $request A request object
     */
    public function executeAddClusters(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Agregar Cluster');
        $this->breadcrumbs = array(array("Administración", "admin/index"), array("Clusters", "adminclusters/index"), array("Agregar Cluster", "adminclusters/addClusters"));
    }

    /**
     * Executes createCluster action
     *
     * @param sfRequest $request A request object
     */
    public function executeCreateCluster(sfWebRequest $request) {


        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('clusters');

        try {

            $tabla = Doctrine::getTable('Clusters');
            $tabla = new Clusters();

            $tabla->name = strtoupper($_arrayPost['name']);

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }


        $this->redirect('/adminclusters/index');
    }

    /**
     * Executes updateCluster action
     * 
     * @param sfWebRequest $request
     * @return type 
     */
    public function executeUpdateCluster(sfWebRequest $request) {

        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('clusters');

        try {

            $tabla = Doctrine::getTable('Clusters')->find(array($_arrayPost["id"]));

            $tabla->name = strtoupper($_arrayPost['name']);

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }

        $this->redirect('/adminclusters/index');
    }

    /**
     * ClusterActive
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateActiveCluster(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $active = $request->getParameter('active');
        $active = (($active == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('Clusters c')
                ->set('c.active', '?', $active)
                ->where('c.id_cluster = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('Clusters c')
                ->orderBy('c.name desc');

        $this->clusters = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');
        $this->setTemplate('lstClusters');
    }

    /**
     * ClusterDelete
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateDeleteCluster(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $deleted = $request->getParameter('deleted');
        $deleted = (($deleted == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('Clusters c')
                ->set('c.deleted', '?', $deleted)
                ->where('c.id_cluster = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('Clusters c')
                ->orderBy('c.name desc');

        $this->clusters = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');
        $this->setTemplate('lstClusters');
    }

}
